package com.haiertvbic.timeline.task;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.widget.ListView;

import com.haiertvbic.timeline.MainActivity;
import com.haiertvbic.timeline.adapter.TimeLineAdapter;
import com.haiertvbic.timeline.bean.ImageInfo;
import com.haiertvbic.timeline.constants.TimeLineConstants;
import com.haiertvbic.timeline.http.HttpDownloader;
import com.haiertvbic.timeline.util.TimeLineUtil;
import com.haiertvbic.tvnow.network.EPGWeb;
import com.haiertvbic.tvnow.network.ImageUtil;

public class TimeLineTask extends AsyncTask<Void, Void, LinkedList<ImageInfo>> {
	private ListView mListView;
	private LinkedList<ImageInfo> mLinkedList;
	private HashMap<String, ImageInfo> mImageIdMap;
	private Context mContext;
	private Handler mHandler;

	private String channelId;
	private String picID;
	private String slide;

	public TimeLineTask(Context context, Handler handler, ListView listView,
			LinkedList<ImageInfo> linkedList,
			HashMap<String, ImageInfo> imageIdMap, String channelId,
			String picID, String slide) {
		this.mListView = listView;
		this.mLinkedList = linkedList;
		this.mImageIdMap = imageIdMap;
		this.mContext = context;
		this.mHandler = handler;
		this.channelId = channelId;
		this.picID = picID;
		this.slide = slide;
	}

	@Override
	protected LinkedList<ImageInfo> doInBackground(Void... params) {
		// String xmlSrc = HttpDownloader.download(TimeLineConstants
		// .getPIPImageListURL(channelId, 5, picID));
		String xmlSrc = EPGWeb.getPIPString(channelId, 5, picID, slide);
		List<?> timeLineImageInfos = null;
		try {
			timeLineImageInfos = TimeLineUtil.parseNETXML(xmlSrc);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if (timeLineImageInfos == null)
			return mLinkedList;
		for (int i = 0; i < timeLineImageInfos.size(); i++) {
			ImageInfo imageInfo = (ImageInfo) timeLineImageInfos.get(i);
			InputStream is = null;
			try {
				is = HttpDownloader.getInputStreamFromUrl(imageInfo
						.getImageUrl() + "&width=300");
				is = ImageUtil.decipheringPic(is);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (is != null)
				imageInfo.setImageBitmap(BitmapFactory.decodeStream(is));
			if (mImageIdMap != null
					&& !mImageIdMap.containsKey(imageInfo.getImageId())) {
				mImageIdMap.put(imageInfo.getImageId(), null);
				if ("right".equalsIgnoreCase(slide)) {
					mLinkedList.addFirst(imageInfo);
				} else {
					mLinkedList.addLast(imageInfo);
				}
			}
			if (mLinkedList.size() > TimeLineConstants.LIST_SHOW_ITEM_SIZE) {
				mLinkedList.removeLast();
			}
		}
		return mLinkedList;
	}

	@Override
	protected void onPostExecute(LinkedList<ImageInfo> result) {
		if (result != null && result.size() > 0) {
			mListView.setAdapter(new TimeLineAdapter(mContext, result));
			MainActivity.mFlushFlag = true;
			mHandler.obtainMessage(TimeLineConstants.PROGRESS_LAYOUT_DISMISS,
					TimeLineConstants.DATA_UPDATE_SUCCESS, 0).sendToTarget();
		} else {
			mHandler.obtainMessage(TimeLineConstants.PROGRESS_LAYOUT_DISMISS,
					TimeLineConstants.DATA_UPDATE_FAIL, 0).sendToTarget();
		}

	}

}
